Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
debakarr
GitHub Repository: debakarr/machinelearning
Path: blob/master/Part 9 - Dimension Reduction/Linear Discriminant Analysis/[R] Linear Discriminant Analysis.ipynb
1341 views
Kernel: R

Linear Discriminant Analysis

Data preprocessing

# Import the dataset dataset = read.csv('Wine.csv')
head(dataset, 10)
# Splitting the dataset into the Training set and Test set library(caTools) set.seed(42) split = sample.split(dataset$Customer_Segment, SplitRatio = 0.8) training_set = subset(dataset, split == TRUE) test_set = subset(dataset, split == FALSE)
head(training_set, 10)
head(test_set, 10)
# Feature Scaling training_set[-14] = scale(training_set[-14]) test_set[-14] = scale(test_set[-14])
head(training_set, 10)
head(test_set, 10)

Applying Linear Discriminant Analysis

library(MASS)
lda = lda(formula = Customer_Segment ~ ., data = training_set) training_set = as.data.frame(predict(lda, training_set))
head(training_set, 10)
training_set = training_set[c(5, 6, 1)] # Reordering the columns
head(training_set, 10)
test_set = as.data.frame(predict(lda, test_set)) test_set = test_set[c(5, 6, 1)] # Reordering the columns
head(test_set, 10)

Fitting classifier to the Training set

library(e1071) classifier = svm(formula = class ~ ., data = training_set, type = 'C-classification', kernel = 'radial')

Predicting the Test set results

y_pred = predict(classifier, newdata = test_set[-3])
head(y_pred, 10)
head(test_set[3], 10)

Making the Confusion Matrix

cm = table(test_set[, 3], y_pred)
cm
y_pred 1 2 3 1 12 0 0 2 0 14 0 3 0 1 9

classifier made 12 + 14 + 9 = 35 correct prediction and 1 incoreect prediction.


Visualizing the Training set results

# install.packages('ElemStatLearn') library(ElemStatLearn)
set = training_set
X1 = seq(min(set[, 1]) - 1, max(set[, 1]) + 1, by = 0.01) X2 = seq(min(set[, 2]) - 1, max(set[, 2]) + 1, by = 0.01) grid_set = expand.grid(X1, X2) colnames(grid_set) = c('x.LD1', 'x.LD2') y_grid = predict(classifier, newdata = grid_set) plot(set[, -3], main = 'Kernel SVM (Training set)', xlab = '1st Linear Discriminant Component', ylab = '2nd Linear Discriminant Component', xlim = range(X1), ylim = range(X2)) contour(X1, X2, matrix(as.numeric(y_grid), length(X1), length(X2)), add = TRUE) points(grid_set, pch = '.', col = ifelse(y_grid == 2, 'lightblue', ifelse(y_grid == 1, 'springgreen3', 'tomato'))) points(set, pch = 21, bg = ifelse(set[, 3] == 2, 'blue3', ifelse(set[, 3] == 1, 'green4', 'red3')), col='white') legend("topright", legend = c("0", "1", "2"), pch = 16, col = c('red3', 'green4', 'blue3'))
Image in a Jupyter notebook

Visualizing the Test set results

set = test_set
X1 = seq(min(set[, 1]) - 1, max(set[, 1]) + 1, by = 0.01) X2 = seq(min(set[, 2]) - 1, max(set[, 2]) + 1, by = 0.01) grid_set = expand.grid(X1, X2) colnames(grid_set) = c('x.LD1', 'x.LD2') y_grid = predict(classifier, newdata = grid_set) plot(set[, -3], main = 'Kernel SVM (Test set)', xlab = '1st Linear Discriminant Component', ylab = '2nd Linear Discriminant Component', xlim = range(X1), ylim = range(X2)) contour(X1, X2, matrix(as.numeric(y_grid), length(X1), length(X2)), add = TRUE) points(grid_set, pch = '.', col = ifelse(y_grid == 2, 'lightblue', ifelse(y_grid == 1, 'springgreen3', 'tomato'))) points(set, pch = 21, bg = ifelse(set[, 3] == 2, 'blue3', ifelse(set[, 3] == 1, 'green4', 'red3')), col='white') legend("topright", legend = c("0", "1", "2"), pch = 16, col = c('red3', 'green4', 'blue3'))
Image in a Jupyter notebook